//
//  LBCropViewController.m
//  LBCropImagePage
//
//  Created by mac on 2024/6/9.
//

#import "LBCropViewController.h"
#import "LBCropView.h"
#import "LBCropPanelView.h"
#import "LBCropConfig.h"



#define IS_IPHONE_X ({\
BOOL iphonex = NO;\
if (@available(iOS 11.0, *)) {\
CGFloat height = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom;\
if (height > 0) {\
iphonex = YES;\
}\
}\
(iphonex);})\



@interface LBCropViewController ()<LBCropPanelViewDelegate>

@property (nonatomic, strong) LBCropView *cropView;
@property (nonatomic, strong) LBCropConfig *config;
@property (nonatomic, strong) LBCropPanelView *panel;


@end

@implementation LBCropViewController

- (instancetype)initWithConfig:(LBCropConfig *)config
{
    if (self = [super init]) {
        self.config = config;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIEdgeInsets margin = UIEdgeInsetsMake(kTopBarSafeHeight, 16, 222 + 40, 16);
    self.cropView = [[LBCropView alloc] initWithFrame:self.view.bounds margin:margin config:self.config];
    self.panel = [[LBCropPanelView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 222, CGRectGetWidth(self.view.bounds), 222)
                                                  ratio:self.config.cropRatioType
                                                  angle:self.config.angle
                                                 rotate:self.config.originAngle];
    self.panel.delegate = self;
    [self.view addSubview:self.cropView];
    [self.view addSubview:self.panel];
    // Do any additional setup after loading the view.
}

#pragma mark - LBCropPanelViewDelegate

- (void)cropPanel:(LBCropPanelView *)cropPanel roate:(CGFloat)rotate
{
    CGFloat angle = rotate * 90 - 45;
    [self.cropView rotate:angle];
}

- (void)cropWithRatioType:(kCropRatioType)ratioType
{
    CGSize ratio;
    switch (ratioType) {
        case kCropRatioType11:
            ratio = CGSizeMake(3, 3);
            break;
        case kCropRatioType34:
            ratio = CGSizeMake(3, 4);
            break;
        case kCropRatioType43:
            ratio = CGSizeMake(4, 3);
            break;
        case kCropRatioType916:
            ratio = CGSizeMake(9, 16);
            break;
        default:
            ratio = self.config.image.size;
            break;
    }
    [self.cropView updateCropWHRatio:(ratio.width/ratio.height)
                       idleGridCount:BMGridCountMake(3, 3)
                     rotateGridCount:BMGridCountMake(3, 3)
                            animated:NO];
}

- (void)panelDidCompleteWithPanelType:(NSString *)panelType
{
    UIImage *image = [self.cropView cropWithCompressScale:1];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)panelDidCancelWithPanelType:(NSString *)panelType
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rotateRight
{
    [self.cropView rotateRight:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
